package com.longrise.bymodule.page.customermanage;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.webkit.WebView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.bymodule.R;
import com.longrise.bymodule.constant.ByModuleConstants;
import com.longrise.bymodule.page.importcontacts.ContactsUpdateEvent;
import com.longrise.bymodule.page.importcontacts.ImportContactsActivity;
import com.longrise.common.base.web.BaseWebActivity;
import com.longrise.common.base.web.BaseWebBridge;
import com.longrise.common.base.web.BaseWebClient;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.publicpage.TipsActivity;
import com.longrise.common.router.RouterPage;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.NetUtil;
import com.longrise.common.utils.PrintLog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Administrator on 2017/11/28.
 * 新框架下的客户管理
 */

@Route(path = RouterPage.ClientManageActivity2)
public class ClientManageActivity2 extends BaseWebActivity {

    private WebView mWv;
    private String mFirstUrl;
    private String mUserid;  //保易的userid
    private boolean mFlag;
    private String mByjkUserid; //保易健康的userid
    private String mByjkCardno;
    private String mByjkMobilephone;
    private String mByjkUsername;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.app_web_comment;
    }

    @Override
    public WebView getWebView() {
        return mWv;
    }

    @Override
    public void init() {
        mWv = (WebView) findViewById(R.id.comment_wv);
        setWebTitle("保易网");
        setToolbarBackIcon(R.drawable.umeng_by_ic_close_black);
        mFlag = true;
        setInterceptBackKey(false);
        EventBus.getDefault().register(this);
    }

    @Override
    public void initData() {
        mByjkUserid = getIntent().getStringExtra("userid");
        mByjkCardno = getIntent().getStringExtra("cardno");
        mByjkMobilephone = getIntent().getStringExtra("mobilephone");
        mByjkUsername = getIntent().getStringExtra("username");
        request();
    }

    @Override
    public BaseWebBridge getwebBridge() {
        return new ClientManagerWebBridge(this, mRxmanager);
    }

    @Override
    public BaseWebClient getWebClient() {
        return new ClientManagerWebClient(this);
    }

    @Override
    public void onToolbarBackClick() {
        super.onToolbarBackClick();
        finish();
    }

    @Override
    public void onToolbarRightIvClick() {
        super.onToolbarRightIvClick();
//        PrintLog.e("BYZPActivity","1111111111111111");
        toShare();
    }


    public void setImageVisible(boolean isVisiable, String title) {
        if (isVisiable) {
            setToolbarRightIvTitle(R.drawable.by_ic_share);
        } else {
            hideToolbarRightIvTitle();
        }
        showTips(title);
    }

    private void request() {
        int screenDp = 0;
        try {
            int topHeight = AppUtil.getStatusBar(this);
            int screenHeight = AppUtil.getScreenHeight();
            screenDp = AppUtil.px2dip(screenHeight - topHeight);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EntityBean entityBean = new EntityBean();
        entityBean.set("userid", mByjkUserid);
        entityBean.set("cardno", mByjkCardno);
        entityBean.set("mobilephone", mByjkMobilephone);
        entityBean.set("username", mByjkUsername);
        entityBean.set("screenheight", screenDp - 45);
        entityBean.set("page", 1);
        EntityBean reqBean = new EntityBean();
        reqBean.set("bean", entityBean);
        LoadDataManager.getInstance().callService(null, ByModuleConstants.BY_URL, "app_isauthorization", reqBean, new LoadDataManager.OnRequestCallListener() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    clientParse((EntityBean) result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                mUrl = "";
            }

            @Override
            public void onFinished(String key, String service) {
            }
        });
    }

    private void clientParse(EntityBean result) {
        if (result == null) {
            mUrl = "";
            return;
        }
        EntityBean bean = result.getBean("result");
        mUrl = bean.getString("href");
        //---xkj 增加userid--
        mFirstUrl = mUrl;
        mUserid = bean.getString("userid");
        //------
        if (isEmptyUrl()) {
            loadUrl(mUrl);
        }
    }

    private boolean isEmptyUrl() {
        if (TextUtils.isEmpty(mUrl)) {
            mUrl = "";
            return false;
        } else {
            return true;
        }
    }

    public void startContactActivity() {
        Intent intent = new Intent(this, ImportContactsActivity.class);
        intent.putExtra(ImportContactsActivity.BY_USERID, mUserid);
        startActivity(intent);
    }

    /**
     * 添加联系人页面传过来的事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void toRefresh(ContactsUpdateEvent bean) {
        ContactsUpdateEvent.UpdateType mType = bean.mType;
        if (mType == ContactsUpdateEvent.UpdateType.update) {
            if (!TextUtils.isEmpty(mFirstUrl)) {  //重新刷新 显示最新的联系人
                if (NetUtil.checkNetEnable()) {
                    loadUrl(mFirstUrl);
                }
            }
        } else if (mType == ContactsUpdateEvent.UpdateType.close) {
            finish();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 新手提示
     */
    public void showTips(String title) {
//        暂时不需要判断,每次进来都需要提示.
//        boolean b = Control.isbyShare();
//        if(!b){
//          return ;
//        }
        if (!mFlag) {
            return;
        }
        boolean e = "保易网需求分析测试".equals(title);
        PrintLog.e("BYZPActivity", "e=:" + e);
        if (e) {
            int[] loc1 = new int[2];
            int width = AppUtil.dip2px(15);
            int height = AppUtil.dip2px(37);
            PrintLog.e("BYZPActivity", "width=:" + width);
            PrintLog.e("BYZPActivity", "height=:" + height);
            int h1 = AppUtil.dip2px(20);
            int com = AppUtil.dip2px(8);
//            int[] loc = {loc1[0] - com, loc1[1] - h1 - com, loc1[0] + width + com, loc1[1] + height - h1 + com};
            int[] loc = {AppUtil.getScreenWidth() - AppUtil.dip2px(15) - width, AppUtil.dip2px(12), loc1[0] + width + com, loc1[1] + height - h1 + com};
            PrintLog.e("BYZPActivity", "loc=:" + loc[0]);
            Intent intent = new Intent(this, TipsActivity.class);
            intent.putExtra("type", TipsActivity.BYSHARE_TIPS);
            intent.putExtra("location", loc);
            startActivity(intent);
            mFlag = false;
            PrintLog.e("BYZPActivity", "3333333333333333");
        }
    }
}
